package android.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/*
 * klasa za kreiranje na bazata na podatoci
 */
public class DBAdapter {
	//definiranje na konstanti koi ke se koristat pri kreiranje na bazata 
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_DATE = "date";
    public static final String KEY_SCORE = "score";
    private static final String TAG = "DBAdapter";
    
    private static final String DATABASE_NAME = "MyDB";
    private static final String DATABASE_TABLE = "rezultati";
    private static final int DATABASE_VERSION = 3;
//konstnta koja sodrzi SQL naredba za kreiranje na tabela rezultati vo bazata MyDB
    private static final String DATABASE_CREATE =
        "create table rezultati (_id integer primary key autoincrement, "
        + "name text not null, date text not null, score text not null);";
        
    private final Context context;    

    private DatabaseHelper DBHelper;//pomosna klasa vo android zo upravuvanje i kreiranje na bazata
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
        /*
         * pomosna klasa vo android zo upravuvanje i kreiranje na bazata
         */
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
        	try {
        		//kreiranje na bazata
        		db.execSQL(DATABASE_CREATE);	
        	} catch (SQLException e) {
        		e.printStackTrace();
        	}
        }

        @Override
        /*
         * f-ja koja se povikuva pri nadograduvanje na bazata
         * so proveruvanje na vrednosta definirana vo DATABASE_VERZION konstantata
         */
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS rezultati");
            onCreate(db);
        }
    }    

    //---otvoranje na bazata---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---zatvaranje na bazata---    
    public void close() 
    {
        DBHelper.close();
    }
    
    //---vnesuvanje na nov rezultat vo bazata---
    public long insertScore(String name, String date, String score) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_DATE, date);
        initialValues.put(KEY_SCORE, score);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---vrakanje na site rezultati sto gi ima vo bazata---
    public Cursor getAllScores() 
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                KEY_DATE, KEY_SCORE}, null, null, null, null, null);
    }
}
